//
//  DMHomeViewController.m
//  MCNKuaiYi
//
//  Created by Mr.Cannon on 2017/1/7.
//  Copyright © 2017年 Mr.Cannon. All rights reserved.
//

#import "DMHomeViewController.h"
#import "DMDiseaseView.h"
#import "DMMarqueeView.h"
#import "DMWeatherView.h"
#import "DMFamousDoctorView.h"
#import "DMAppointmentController.h"
#import "DMCommonwealViewController.h"
#import "DMWeatherModel.h"
@interface DMHomeViewController ()<DMDiseaseViewDelegate>
//创建疾病属性
@property(nonatomic,strong)DMDiseaseView* disease;
//创建跑马灯属性
@property(nonatomic,strong)DMMarqueeView* marquee;
//创建天气属性
@property(nonatomic,strong)DMWeatherView* weather;
//创建名医滚动视图
@property(nonatomic,strong)DMFamousDoctorView* famousDoctor;
@end

@implementation DMHomeViewController

//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    DMAppointmentController *ac = [[DMAppointmentController alloc]init];
//    
//    [self.navigationController pushViewController:ac animated:YES];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = DMRGBCOLOR;
    [self setupUI];
    [self loadWeather];
}
#pragma mark - 初始化界面
-(void)setupUI
{
    
    //创建天气视图
    DMWeatherView* weatherView= [[DMWeatherView alloc]init];
    //创建名医无线轮播视图
    DMFamousDoctorView* famousDoctor = [[DMFamousDoctorView alloc]init];
    //创建跑马灯视图
    DMMarqueeView* marqueeView = [[DMMarqueeView alloc]init];
    //创建疾病视图
    DMDiseaseView* diseaseView = [[DMDiseaseView alloc]init];
    //添加到主视图
    [self.view addSubview:weatherView];
    [self.view addSubview:marqueeView];
    [self.view addSubview:famousDoctor];
    [self.view addSubview:diseaseView];
    //设置约束
    [weatherView mas_makeConstraints:^(MASConstraintMaker *make)
     {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(70);
    }];
    [marqueeView mas_makeConstraints:^(MASConstraintMaker *make)
     {
        make.top.equalTo(weatherView.mas_bottom);
        make.left.right.equalTo(weatherView);
        make.height.mas_equalTo(40);
    }];
    [famousDoctor mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.left.right.equalTo(marqueeView);
        make.top.equalTo(marqueeView.mas_bottom).offset(0.5);
        make.height.mas_equalTo(230);
    }];
    [diseaseView mas_makeConstraints:^(MASConstraintMaker *make)
     {
        make.top.equalTo(famousDoctor.mas_bottom).offset(0.5);
        make.right.left.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-44.5);
    }];
    self.disease = diseaseView;
    self.disease.delegate = self;
    self.marquee = marqueeView;
    self.famousDoctor = famousDoctor;
    self.weather = weatherView;
}
#pragma mark - 疾病界面的代理方法
-(void)disease:(DMDiseaseView *)disease andTag:(NSInteger)tag
{
    if(tag!=6)
    {
    NSLog(@"Home - %zd",tag);
    DMAppointmentController *appointmentController = [[DMAppointmentController alloc]init];
    appointmentController.id = tag;
    [self.navigationController pushViewController:appointmentController animated:YES];
    }else
    {
        DMCommonwealViewController* commonwealViewController = [[DMCommonwealViewController alloc]init];
        [self.navigationController pushViewController:commonwealViewController animated:YES];
    }
}
-(void)loadWeather
{
    //天气KEY
    NSString* key = @"17IvzuqmKrK1cGwNL6VQebF9";
    //获取数据
    NSString* urlStr = @"http://api.map.baidu.com/telematics/v3/weather";
   [[DMNetworkTool shareDMNetwork]GETWithURLString:urlStr parameters:@{@"location":@"深圳",@"output":@"json",@"ak":key} success:^(id responseObject)
    {
        //反序列化
//        NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
//        
        DMWeatherModel* weatherModel = [[DMWeatherModel alloc]init];
        NSArray *result = responseObject[@"results"];
        NSDictionary *resultDict = result.firstObject;
        NSArray *weatherData = resultDict[@"weather_data"];
        NSDictionary *weatherDataDict = weatherData.firstObject;
        weatherModel = [DMWeatherModel weatherWithDict:weatherDataDict];
        self.weather.model = weatherModel;
    
   } faile:^(NSError *error) {
       NSLog(@"网络请求失败");
   }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
